use polars::prelude::*;
use calamine::{open_workbook, Xlsx, Reader};

pub fn main() {

}


// 读取 Excel 到 DataFrame
fn read_excel_to_df(path: &str) -> Result<DataFrame> {
    let mut workbook: Xlsx<_> = open_workbook(path)?;
    let sheet_names = workbook.sheet_names();

    // 读取第一个工作表
    if let Some(Ok(range)) = workbook.worksheet_range(&sheet_names[0]) {
        let (height, width) = range.get_size();
        let mut columns: Vec<Series> = Vec::with_capacity(width);

        // 按列处理数据
        for col_idx in 0..width {
            let mut col_data: Vec<AnyValue> = Vec::with_capacity(height);
            for row_idx in 0..height {
                let cell = range.get_value((row_idx, col_idx));
                col_data.push(match cell {
                    Data::String(s) => AnyValue::String(s),
                    Data::Float(f) => AnyValue::Float64(f),
                    Data::Int(i) => AnyValue::Int64(i),
                    Data::Bool(b) => AnyValue::Boolean(b),
                    _ => AnyValue::Null,
                });
            }
            columns.push(Series::new(&format!("col_{}", col_idx), col_data));
        }
        Ok(DataFrame::new(columns)?)
    } else {
        Err(PolarsError::NoData("工作表读取失败".into()))
    }
}

// 写入 DataFrame 到 Excel
fn write_df_to_excel(df: &DataFrame, path: &str) -> Result<()> {
    let mut file = std::fs::File::create(path)?;
    ExcelWriter::new(&mut file)
        .with_sheet_name("Sheet1")
        .finish(df)
}
